<?php

foreach ($villou as $city){
    echo '<input class="citiesHidden" type="hidden" value="'.$city['PlaceCity']['places_id'].'@'.$city['PlaceCity']['name'].'"/>';
}
echo $this->Html->script('jquery-1.7.1.js');
?>



<h2>Localisation</h2>

<?php
    echo $this->Form->create();
    echo $this->Form->input('content', array(
        'label' => 'Search a place'
    ));
    echo $this->Form->end('Search');
    ?>
    <div id="map_canvas"></div>
    <?php
    if (!empty($results)){
       
        $cities = $results['PlaceCity'];
        $departments = $results['PlaceDepartment'];
        $countries = $results['PlaceCountry'];
        $states = $results['PlaceState'];
        ?>


        <?php
        if (!empty($cities)){
            ?>
            <h3> Les villes trouvées </h3>
            <ul>
            <?php
            foreach ($cities as $placeCity){
                echo '<input class="citiesHidden2" type="hidden" value="'.$placeCity['PlaceCity']['places_id'].'@'.$placeCity['PlaceCity']['name'].'"/>';
                $city = $placeCity['PlaceCity'];

            ?>
                <li><?php $this->Display->link($city, 'PlaceCity');?></li>
            <?php
            }
            ?>
            </ul>
        <?php
        }
        
        if (!empty($departments)){
            ?>
            <h3> Les départements trouvés </h3>
            <ul>
            <?php
            foreach ($departments as $department){
                $department = $department['PlaceDepartment'];
                ?>
                <li><?php $this->Display->link($department, 'PlaceDepartment');?></li>
            <?php
            }
            ?>
            </ul>
                
        <?php
        }
        
        if (!empty($states)){
            ?>
            <h3> Les régions trouvés </h3>
            <ul>
            <?php
            foreach ($states as $state){
                $state = $state['PlaceState'];

            ?>
                <li><?php $this->Display->link($state, 'PlaceState');?></li>
            <?php
            }
            ?>
              
            </ul>  
        <?php
        }
        if (!empty($countries)){
            ?>
            <h3> Les Pays trouvés </h3>
            <ul>
            <?php
            foreach ($countries as $country){
                $country = $country['PlaceCountry'];

            ?>
                <li><?php $this->Display->link($country, 'PlaceCountry');?></li>
            <?php
            }
            ?>
            </ul>

            <?php
        }
    }
    else{
        ?>
        <p>
            Aucun résultat trouvé pour cette recherche !
        </p>
        <?php
    }
        
?>

<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=true&amp;key=ABQIAAAA9D-zqq9EKpegmTjeNx3bkBT2yXp_ZAY8_ufC3CFXhHIE1NvwkxRGjJAmrQpQMul-LWOUkYHCiwz3VA" type="text/javascript"></script>

    <script type="text/javascript">





    $(document).ready(function(){
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.addControl(new GSmallMapControl()); 
            Geocoder = new GClientGeocoder();
            Geocoder.getLatLng('France', function(latlngFr) {
                if (latlngFr) map.setCenter(latlngFr,6);
             });
      }
        tabCities = {};
        i=0;
        $('.citiesHidden').each(function(index, value){
            tabCities[index] = $(value).val();
      
            i++;
        });
        tabCities2 = {};
        z=0;
        $('.citiesHidden2').each(function(index, value){
            tabCities2[index] = $(value).val();
      
            z++;
        });
        var myIcon = new GIcon(G_DEFAULT_ICON, "http://www.xn--icne-wqa.com/images/icones/2/1/circle_red.png");
        myIcon.iconSize = new GSize(15,15);
        myIcon.dragCrossSize = new GSize(15,15);

        myIcon.shadow = '';
        myIcon.iconAnchor = new GPoint(5,5);

        if(z==0){
          addCity(tabCities,i);  
        }else{
          addCity(tabCities2,z);
        }
        
        

        function mapMarkLatLng (latLng, adress) {
            marker = new GMarker(latLng,{icon : myIcon,clickable: true, draggable: false, title: adress.split('@')[1]});
            map.addOverlay(marker);
            GEvent.addListener(marker, "click", function() {
                window.location='<?php echo $this->webroot;?>PlaceCities/view/'+adress.split('@')[0];
            });

        }
        function mapMarkAdress (adress) {
            mapGeocoder = new GClientGeocoder();
            mapGeocoder.getLatLng(adress.split('@')[1], function(latLng) {
                if (latLng) mapMarkLatLng(latLng, adress);
             });
         }
         function addCity(tabCities,long){
            for(i=0;i<long;i++){
                mapMarkAdress(tabCities[i]);
            }
         }
    });






   
    </script>


